// merchant//merchants/uploadLiense/index.js
const utils = require('../../utils/util.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    license: [],//营业执照
    showLicense: [],//渲染营业执照
  },

  toAddProduct: function(){
    let that = this;
    let staffid = wx.getStorageSync('cardId');
    if(that.data.license.length>0){
      wx.request({
        url: app.globalData.host + '/api/staff/SetLicense?staffid='+ staffid +'&content=' + that.data.license[0],
        success: function(res){
          if(res.data.Code == 0){
            wx.navigateTo({
              url: '../addProduct/index?seltoid=' + app.globalData.userInfo.Uid,
            })
          }else{
            wx.showModal({
              title: '提示',
              content: res.data.Message,
              showCancel: false
            })
          }
        }
      })
    }else{
      wx.showToast({
        title: '请上传营业执照',
        mask: true,
        duration: 1000
      })
    }
  },

  toPreStep: function(){
    wx.navigateBack({
      delta: 1
    })
  },

  uploadLiense: function(){
    var that = this;
    var detailPics = [];
    detailPics = that.data.license;
    var pics = [];
    if (detailPics.length >= 1) {
        wx.showToast({
          title: '最多选择1张！',
        })
        return;
      }
      wx.chooseImage({
        count: 1, // 最多可以选择的图片张数，默认9
        sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
        sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
        success: function (res) {
          var imgs = res.tempFilePaths;
          pics.push(imgs[0]);
          that.uploadimgs({
            url: app.globalData.host + '/api/tool/upload?operation=uploadcouponimage', //这里是你图片上传的接口
            path: pics, //这里是选取的图片的地址数组
          });
        },
      })
    },

    uploadimgs: function (data) {
      wx.showLoading({
        title: '提交中...',
        mask: true,
      })
      var that = this,
        i = data.i ? data.i : 0,
        success = data.success ? data.success : 0,
        fail = data.fail ? data.fail : 0;
      wx.uploadFile({
        url: data.url,
        filePath: data.path[i],
        name: 'fileData',
        formData: null,
        success: (resp) => {
          wx.hideLoading();
          success++;
          var str = resp.data
          var pic = JSON.parse(str);
          var pic_name = pic.Data;
          let license = that.data.license;
            license.push(pic_name);
            let showLicense = license.concat();
            for (let i = 0; i < showLicense.length; i++) {
              showLicense[i] = utils.formatAvater(showLicense[i], 'https://static.711688.net.cn/upload/coupondetail/');
            }
            that.setData({
              license: license,
              showLicense: showLicense
            })
        },
        fail: (res) => {
          fail++;
          console.log('fail:' + i + "fail:" + fail);
        },
        complete: () => {
          i++;
          if (i == data.path.length) { //当图片传完时，停止调用     
            console.log('成功：' + success + " 失败：" + fail);
          } else { //若图片还没有传完，则继续调用函数
            data.i = i;
            data.success = success;
            data.fail = fail;
            that.uploadimgs(data);
          }
        }
      });
    },

  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  }
})